package com.lyt.springboot.util.algorithm;

public class S26 {
    public int solve (char[][] grid) {
        int res = 0;
        //二维数组判空
        if(grid ==  null || grid.length == 0 ||(grid.length == 1 && grid[0].length == 0)) {
            return 0;
        }
        if(grid.length == 1 && grid[0].length ==1) {
            int a = grid[0][0] == 1?1:0;
            return a;
        }
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == 1) {
                    res++;
                    dfs(grid,i,j);
                }

            }

        }
        return res;
    }
    public void dfs(char[][] grid,int i,int j) {
        if(i<0 || i>=grid.length || j<0 || j>=grid[0].length || grid[i][j] == 2) {
            return;
        }
        if(grid[i][j] == 1) {
            grid[i][j] = 2;
            dfs(grid,i+1,j);
            dfs(grid,i-1,j);
            dfs(grid,i,j+1);
            dfs(grid,i,j-1);
        }
    }

    public static void main(String[] args) {
        char[][] arr = {{1},{0},{1},{0},{1},{1},{0}};
//        char[][] arr = {{1}};
        int []  arr2 = {1,2,3,4};
        int[][] arr1 = {{3,2,3},{2,3,4},{4,5,6}};
        arr1[0][1] = 3;
        int b =  arr2[0] == 1 ? 1:0;
        int a  = arr1[0][0] == 3 ? 1:0;
        int c= arr[0][0] == '1' ? 1000:0;

        S26 s26 = new S26();
        int solve = s26.solve(arr);
        System.out.println(solve);
    }
}
